package com.fastloan.mgt.model.uitl;

import com.fastloan.mgt.model.annotation.ModelClass;
import com.fastloan.mgt.model.model.Model;
import com.fastloan.mgt.model.service.GeneratorModelStrategy;

import java.math.BigDecimal;

/**
 * @author yanwe
 * createTime 2017-11-2017/11/30 11:16
 */
public class ModelUtil {

    /**
     * 处理数字为整数
     *
     * magnitude量级选择为 2 , roundingMode选择为BigDecimal.ROUND_UP 时效果如下
     * 111111 =  120000
     * 11111 = 11111
     *
     * @param bigDecimal   数字
     * @param magnitude    量级
     * @param roundingMode 模式
     * @return 处理后的数字
     */
    public static BigDecimal disposeResult(BigDecimal bigDecimal, int magnitude, int roundingMode) {
        String s = bigDecimal.setScale(0, BigDecimal.ROUND_UP).toString();
        if (new BigDecimal(s).equals(BigDecimal.ZERO)) {
            return BigDecimal.ZERO;
        }
        int length = s.length();
        return bigDecimal
                .divide(new BigDecimal(10).pow(length - magnitude), 0, roundingMode)
                .multiply(new BigDecimal(10).pow(length - magnitude));
    }

    /**
     * 转换模型类到模型对象
     *
     * @param object 模型类
     * @return 模型对象
     */
    public static Model convertModel(Object object){
        ModelClass annotation = object.getClass().getAnnotation(ModelClass.class);
        return Model.ModelBuilder.aModel().withModelHandler((GeneratorModelStrategy) object)
                .withModelId(annotation.modelId())
                .withModelName(annotation.modelName()).build();
    }
}
